﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using BLL;

public partial class Admin_ModifyPlanMasterPeople : System.Web.UI.Page
{
    BLL.QuarterDateManager dllQuarterDateManager = new BLL.QuarterDateManager();
    BLL.MonthDateManager dllMonthDateManager = new BLL.MonthDateManager();
    BLL.WeekDateManager dllWeekDateManager = new BLL.WeekDateManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDropDownList(this.drpYear);
            bindDropDownList1(this.drpYearEnd);
            bindDropDownList(this.drpQuarterDate);
            bindDropDownList1(this.drpQuarterDateEnd);
            bindDropDownList(this.drpMonthDate);
            bindDropDownList1(this.drpMonthDateEnd);
            bindDropDownList(this.drpWeekDate);
            bindDropDownList1(this.drpWeekDateEnd);
            Bind();
        }
    }


    void Bind()
    {
        BLL.DepartmentInfoManager dllDepartment = new BLL.DepartmentInfoManager();
        this.drpDepartment.DataSource = dllDepartment.getNoTree();
        this.drpDepartment.DataTextField = "DepartmentName";
        this.drpDepartment.DataValueField = "DepartmentId";
        this.drpDepartment.DataBind();
        ListItem _li = new ListItem("请选择部门");
        this.drpDepartment.Items.Insert(0, _li);
    }

    void bindDropDownList(DropDownList _drp)
    {
        //年份绑定
        if (_drp.Equals(this.drpYear) || _drp.Equals(this.drpYearEnd))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem _li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(_li);
            }

            //根据系统当前日期查找到对应的年周期
            if (Request.QueryString["Year"] == null)
            {
                DataTable _year = dllQuarterDateManager.getQuarterDate();
                _drp.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year.Rows[0][2].ToString()));
            }
            else
            {
                _drp.SelectedValue = Utility.HttpHelp.getParamsString("Year");
            }

        }

        //绑定年计划周期
        if (_drp.Equals(this.drpQuarterDate))
        {
            object[] _value = new object[1];
            _value[0] = this.drpYear.SelectedValue;
            DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
            if (_quarter.Rows.Count != 0)
            {
                _drp.DataSource = _quarter;
                _drp.DataTextField = "QuarterName";
                _drp.DataValueField = "Quarter_Id";
                _drp.DataBind();

                if (Request.QueryString["QuarterDate"] == null)
                {
                    #region dropdownlist显示系统所对应的当前年周期
                    //根据系统当前日期查找到对应的年周期
                    DataTable _quartertime = dllQuarterDateManager.getQuarterDate();
                    //如果查到结果（结果只有一条）

                    if (_quartertime.Rows.Count == 1)
                    {
                        _drp.SelectedIndex = _drp.Items.IndexOf(_drp.Items.FindByText(_quartertime.Rows[0][1].ToString()));
                        _drp.DataSource = _quartertime;
                        _drp.DataTextField = "QuarterName";
                        _drp.DataValueField = "Quarter_Id";
                    }
                    #endregion
                }
                else
                {
                    _drp.SelectedValue = Utility.HttpHelp.getParamsString("QuarterDate");
                }
            }
            else
            {
                ListItem _li = new ListItem("无年周期", "0");
                _drp.Items.Clear();
                _drp.Items.Add(_li);
            }

        }

        //绑定月计划周期
        if (_drp.Equals(this.drpMonthDate))
        {
            object[] _value = new object[2];
            _value[0] = this.drpYear.SelectedValue;
            _value[1] = this.drpQuarterDate.SelectedItem.Text;
            DataTable _month = dllMonthDateManager.get(MonthDateManager.field.QuarterName.ToString(), _value);
            if (_month.Rows.Count != 0)
            {
                _drp.DataSource = _month;
                _drp.DataTextField = "MonthName";
                _drp.DataValueField = "Month_Id";
                _drp.DataBind();
                if (Request.QueryString["MonthDate"] == null)
                {
                    #region 根据系统当前日期绑定到当前月周期
                    DataTable _monthtime = dllMonthDateManager.getMonthDate();
                    if (_monthtime.Rows.Count > 0)
                    {
                        _drp.SelectedIndex = _drp.Items.IndexOf(_drp.Items.FindByText(_monthtime.Rows[0][3].ToString()));

                    }
                    #endregion
                }
                else
                {
                    _drp.SelectedValue = Utility.HttpHelp.getParamsString("MonthDate");
                }
            }
            else
            {
                ListItem _li = new ListItem("无月周期", "0");
                _drp.Items.Clear();
                _drp.Items.Add(_li);

            }

        }
        //绑定周计划周期
        if (_drp.Equals(this.drpWeekDate))
        {
            object[] _value = new object[3];
            _value[0] = this.drpYear.SelectedValue;
            _value[1] = this.drpQuarterDate.SelectedItem.Text;
            _value[2] = this.drpMonthDate.SelectedItem.Text;
            DataTable _week = dllWeekDateManager.get(WeekDateManager.field.MonthName.ToString(), _value);
            if (_week.Rows.Count != 0)
            {
                _drp.DataSource = _week;
                _drp.DataTextField = "WeekName";
                _drp.DataValueField = "Week_Id";
                _drp.DataBind();
                if (Request.QueryString["WeekDate"] == null)
                {
                    DataTable _weektime = dllWeekDateManager.getWeekDate();
                    if (_weektime.Rows.Count > 0)
                    {
                        _drp.SelectedIndex = _drp.Items.IndexOf(_drp.Items.FindByText(_weektime.Rows[0][4].ToString()));


                    }
                }
                else
                {
                    _drp.SelectedValue = Utility.HttpHelp.getParamsString("WeekDate");
                }
            }
            else
            {
                ListItem _li = new ListItem("无周周期", "0");
                _drp.Items.Clear();
                _drp.Items.Add(_li);
            }
        }
    }

    void bindDropDownList1(DropDownList _drp)
    {
        //年份绑定
        if (_drp.Equals(this.drpYearEnd))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem _li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(_li);
            }

            //根据系统当前日期查找到对应的年周期
            if (Request.QueryString["Year"] == null)
            {
                DataTable _year = dllQuarterDateManager.getQuarterDate();
                _drp.SelectedIndex = _drp.Items.IndexOf(_drp.Items.FindByText(_year.Rows[0][2].ToString()));
            }
            else
            {
                _drp.SelectedValue = Utility.HttpHelp.getParamsString("Year");
            }

        }

        //绑定年计划周期
        if (_drp.Equals(this.drpQuarterDateEnd))
        {
            object[] _value = new object[1];
            _value[0] = this.drpYearEnd.SelectedValue;
            DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
            if (_quarter.Rows.Count != 0)
            {
                _drp.DataSource = _quarter;
                _drp.DataTextField = "QuarterName";
                _drp.DataValueField = "Quarter_Id";
                _drp.DataBind();

                if (Request.QueryString["QuarterDate"] == null)
                {
                    #region dropdownlist显示系统所对应的当前年周期
                    //根据系统当前日期查找到对应的年周期
                    DataTable _quartertime = dllQuarterDateManager.getQuarterDate();
                    //如果查到结果（结果只有一条）

                    if (_quartertime.Rows.Count == 1)
                    {
                        _drp.SelectedIndex = _drp.Items.IndexOf(_drp.Items.FindByText(_quartertime.Rows[0][1].ToString()));
                        _drp.DataSource = _quartertime;
                        _drp.DataTextField = "QuarterName";
                        _drp.DataValueField = "Quarter_Id";
                    }
                    #endregion
                }
                else
                {
                    _drp.SelectedValue = Utility.HttpHelp.getParamsString("QuarterDate");
                }
            }
            else
            {
                ListItem _li = new ListItem("无年周期", "0");
                _drp.Items.Clear();
                _drp.Items.Add(_li);
            }

        }

        //绑定月计划周期
        if (_drp.Equals(this.drpMonthDateEnd))
        {
            object[] _value = new object[2];
            _value[0] = this.drpYearEnd.SelectedValue;
            _value[1] = this.drpQuarterDateEnd.SelectedItem.Text;
            DataTable _month = dllMonthDateManager.get(MonthDateManager.field.QuarterName.ToString(), _value);
            if (_month.Rows.Count != 0)
            {
                _drp.DataSource = _month;
                _drp.DataTextField = "MonthName";
                _drp.DataValueField = "Month_Id";
                _drp.DataBind();
                if (Request.QueryString["MonthDate"] == null)
                {
                    #region 根据系统当前日期绑定到当前月周期
                    DataTable _monthtime = dllMonthDateManager.getMonthDate();
                    if (_monthtime.Rows.Count > 0)
                    {
                        _drp.SelectedIndex = _drp.Items.IndexOf(_drp.Items.FindByText(_monthtime.Rows[0][3].ToString()));

                    }
                    #endregion
                }
                else
                {
                    _drp.SelectedValue = Utility.HttpHelp.getParamsString("MonthDate");
                }
            }
            else
            {
                ListItem _li = new ListItem("无月周期", "0");
                _drp.Items.Clear();
                _drp.Items.Add(_li);

            }

        }
        //绑定周计划周期
        if (_drp.Equals(this.drpWeekDateEnd))
        {
            object[] _value = new object[3];
            _value[0] = this.drpYearEnd.SelectedValue;
            _value[1] = this.drpQuarterDateEnd.SelectedItem.Text;
            _value[2] = this.drpMonthDateEnd.SelectedItem.Text;
            DataTable _week = dllWeekDateManager.get(WeekDateManager.field.MonthName.ToString(), _value);
            if (_week.Rows.Count != 0)
            {
                _drp.DataSource = _week;
                _drp.DataTextField = "WeekName";
                _drp.DataValueField = "Week_Id";
                _drp.DataBind();
                if (Request.QueryString["WeekDate"] == null)
                {
                    DataTable _weektime = dllWeekDateManager.getWeekDate();
                    if (_weektime.Rows.Count > 0)
                    {
                        _drp.SelectedIndex = _drp.Items.IndexOf(_drp.Items.FindByText(_weektime.Rows[0][4].ToString()));


                    }
                }
                else
                {
                    _drp.SelectedValue = Utility.HttpHelp.getParamsString("WeekDate");
                }
            }
            else
            {
                ListItem _li = new ListItem("无周周期", "0");
                _drp.Items.Clear();
                _drp.Items.Add(_li);
            }
        }
    }

    /// <summary>
    /// 更新评估人
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnModify_Click(object sender, EventArgs e)
    {

        if (this.txtUserName.Value.Length == 0 && this.txtUserNameOld.Value.Length == 0)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('请选择责任人！')</script>");
            return;
        }
        if (this.drpDepartment.SelectedItem.Text == "请选择部门")
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('请选择部门！')</script>");
            return;
        }
        SqlParameter[] parameters = new SqlParameter[9];
        parameters[0] = new SqlParameter("@QuarterDate", Convert.ToInt32(this.drpQuarterDate.SelectedValue));
        parameters[1] = new SqlParameter("@QuarterDateEnd", Convert.ToInt32(this.drpQuarterDateEnd.SelectedValue));
        parameters[2] = new SqlParameter("@MonthDate", Convert.ToInt32(this.drpMonthDate.SelectedValue));
        parameters[3] = new SqlParameter("@MonthDateEnd", Convert.ToInt32(this.drpMonthDateEnd.SelectedValue));
        parameters[4] = new SqlParameter("@WeekDate", Convert.ToInt32(this.drpWeekDate.SelectedValue));
        parameters[5] = new SqlParameter("@WeekDateEnd", Convert.ToInt32(this.drpWeekDateEnd.SelectedValue));
        parameters[6] = new SqlParameter("@MasterDepartment", this.drpDepartment.SelectedItem.Text);
        parameters[7] = new SqlParameter("@MasterPeople", this.txtUserName.Value);
        parameters[8] = new SqlParameter("@MasterPeopleOld", this.txtUserNameOld.Value);
        int val = DAL.SqlHelper.ExecuteNonQuery(DAL.SqlHelper.connStr, CommandType.StoredProcedure, "Update_PlanMasterPeopleByTime", parameters);
        if (val > 0)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('修改成功')</script>");
        }
    }
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpQuarterDate);

    }
    protected void drpQuarterDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpMonthDate);

    }
    protected void drpMonthDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpWeekDate);

    }
    protected void drpQuarterDateEnd_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList1(this.drpMonthDateEnd);
    }
    protected void drpMonthDateEnd_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList1(this.drpWeekDateEnd);

    }

    protected void drpYearEnd_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList1(this.drpQuarterDateEnd);
    }
}